var Block = function(game, position) {
    // positon 是 [0, 0] 格式
    var p = position
    var image = game.imageByName('block')
    var o = {
        image: image,
        x: p[0],
        y: p[1],
        w: image.width,
        h: image.height,
        alive: true,
        lifes: p[2] || 1,
    }
    var changeLook = function(block) {
            if (o.lifes >= 5) {
                o.image = game.imageByName('block2')
            } else if (o.lifes > 2 && o.lifes < 5) {
                o.image = game.imageByName('block')
            } else {
                o.image = game.imageByName('block3')
            }
        }
        // changeLook(o)

    o.kill = function() {
        o.lifes--
            if (o.lifes < 1) {
                o.alive = false
            }
        changeLook(o)
    }
    o.collide = function(guaImage) {
        if (!o.alive) {
            return false
        }
        // 碰撞算法，简单点，判断一个矩形的四个点是否在另一个矩形中
        var left = guaImage.x
        var right = guaImage.x + guaImage.image.width
        var up = guaImage.y
        var down = guaImage.y + guaImage.image.height

        if ((left >= o.x && left < o.x + o.image.width) && (up >= o.y && up < o.y + o.image.height)) {
            return true
        }
        if ((right >= o.x && right < o.x + o.image.width) && (up >= o.y && up < o.y + o.image.height)) {
            return true
        }
        if ((left >= o.x && left < o.x + o.image.width) && (down >= o.y && down < o.y + o.image.height)) {
            return true
        }
        if ((right >= o.x && right < o.x + o.image.width) && (down >= o.y && down < o.y + o.image.height)) {
            return true
        }
        return false
    }

    return o
}